package behavioral.iterator;

import behavioral.iterator.custom.DinerMenu;
import behavioral.iterator.custom.DrinksMenu;
import behavioral.iterator.custom.Iterator;
import behavioral.iterator.java.JuiceMenu;
import behavioral.iterator.java.LunchMenu;

/**
 * 迭代器模式
 * 提供一种方法来顺序访问聚合对象中的一系列数据，而不暴露聚合对象的内部表示。
 */
public class Main {


    public static void main(String[] args) {
        System.out.println(" == 晚餐菜单：");
        DinerMenu dinerMenu = new DinerMenu();
        dinerMenu.addItem("猪肉炖粉条", "猪肉+粉条", 20);
        dinerMenu.addItem("青椒炒肉", "青椒+五花肉", 18);
        dinerMenu.addItem("糖醋里脊", "里脊肉", 21);

        print(dinerMenu.createIterator());

        System.out.println(" == 饮品菜单：");
        DrinksMenu drinksMenu = new DrinksMenu();
        drinksMenu.addItem("豆浆", "黄豆+糖", 15);
        drinksMenu.addItem("奶茶", "奶+糖", 17);
        print(drinksMenu.createIterator());


        System.out.println(" java == 午餐菜单：");
        LunchMenu lunchMenu = new LunchMenu();
        lunchMenu.addItem("蚂蚁上树", "粉条+肉末", 22);
        lunchMenu.addItem("干煸四季豆", "四季豆+猪肉", 22);
        lunchMenu.addItem("宫保鸡丁", "鸡肉+花生米+辣椒", 25);

        print4Java(lunchMenu.createIterator());
        System.out.println(" java == 果汁菜单：");
        JuiceMenu juiceMenu = new JuiceMenu();
        juiceMenu.addItem("苹果汁", "苹果+糖", 15);
        juiceMenu.addItem("雪梨汁", "梨+糖", 17);
        print4Java(juiceMenu.createIterator());
    }

    public static void print(Iterator iterable) {
        while (iterable.hasNext()) {
            System.out.println(iterable.next());
        }
    }

    public static void print4Java(java.util.Iterator iterable) {
        while (iterable.hasNext()) {
            System.out.println(iterable.next());
        }
    }
}
